package com.blockchain.console.service.trading.achieve;

import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.blockchain.common.enums.EntrustType;
import com.blockchain.common.enums.IsPass;
import com.blockchain.common.enums.JyztStatus;
import com.blockchain.common.enums.ZrState;
import com.blockchain.console.service.trading.TradingManage;
import com.blockchain.console.service.trading.entity.CjjlEntity;
import com.blockchain.console.service.trading.entity.CyljEntity;
import com.blockchain.console.service.trading.entity.JyfqEntity;
import com.blockchain.console.service.trading.entity.JyglEntity;
import com.blockchain.console.service.trading.entity.JyscEntity;
import com.blockchain.console.service.trading.entity.XlbEntity;
import com.blockchain.console.service.trading.query.CyljQuery;
import com.blockchain.console.service.trading.query.JyfqQuery;
import com.blockchain.console.service.trading.query.JyscQuery;
import com.blockchain.console.service.trading.query.TradingQuery;
import com.blockchain.console.service.trading.query.XlbQuery;
import com.blockchain.framework.data.sql.SQLConnectionProvider;
import com.blockchain.framework.http.upload.FileStore;
import com.blockchain.framework.http.upload.UploadFile;
import com.blockchain.framework.service.ServiceFactory;
import com.blockchain.framework.service.ServiceResource;
import com.blockchain.framework.service.exception.ParameterException;
import com.blockchain.framework.service.query.ArrayParser;
import com.blockchain.framework.service.query.ItemParser;
import com.blockchain.framework.service.query.Paging;
import com.blockchain.framework.service.query.PagingResult;
import com.blockchain.util.MyCrypt;
import com.blockchain.util.StringHelper;
import com.blockchain.util.parser.DateParser;
import com.blockchain.util.parser.EnumParser;
import com.blockchain.variables.FileType;
import com.blockchain.variables.P2PConst;

public class TradingManageImpl extends AbstractTradingService implements TradingManage {

	public static class TradingManageFactory implements ServiceFactory<TradingManage> {

		@Override
		public TradingManage newInstance(ServiceResource serviceResource) {
			return new TradingManageImpl(serviceResource);
		}
	}

	public TradingManageImpl(ServiceResource serviceResource) {
		super(serviceResource);
	}

	@Override
	public PagingResult<JyglEntity> tradingSearch(TradingQuery tradingQuery, Paging paging) throws Throwable {
		StringBuilder s = new StringBuilder(
				"SELECT T6017.F01 ID,T6010.F02 username,T6017.F04 dj,T6017.F05 sl,T6017.F06 ycjsl,T6017.F07 lx,T6017.F08 zt,T6017.F09 sj, "
						+ "mr.F03 ywm_c,mr.F04 zwm_c, mc.F03 ywm_r,mc.F04 zwm_r,T6017.F02 scid,T6017.F03 userid"
						+ " FROM T6017 LEFT JOIN T6015 ON T6017.F02=T6015.F01 "
						+ " LEFT JOIN T6010 ON T6017.F03=T6010.F01 " + " LEFT JOIN T6013 mr ON mr.F01=T6015.F03 "
						+ " LEFT JOIN T6013 mc ON mc.F01=T6015.F04 " + " WHERE 1=1 ");
		ArrayList<Object> parameters = new ArrayList<>();
		int userid = serviceResource.getSession().getAccountId();
		if (userid == 17) {
			s.append(" AND T6015.F01=6");
		} else {
			if (tradingQuery != null) {
				SQLConnectionProvider sqlConnectionProvider = getSQLConnectionProvider();
				if (tradingQuery.getId() > 0) {
					s.append(" AND T6015.F01=?");
					parameters.add(tradingQuery.getId());
				}
				if (!StringHelper.isEmpty(tradingQuery.getUsername())) {
					s.append(" AND T6010.F02 LIKE ?");
					parameters.add(sqlConnectionProvider.allMatch(tradingQuery.getUsername()));
				}
				if (tradingQuery.getJyztStatus() != null) {
					s.append(" AND T6017.F07=?");
					parameters.add(tradingQuery.getJyztStatus());
				}
				if (tradingQuery.getWtzt() != null) {
					s.append(" AND T6017.F08=?");
					parameters.add(tradingQuery.getWtzt());
				}
				if (tradingQuery.getCreateTimeStart() != null) {
					s.append(" AND DATE(T6017.F09) >= ?");
					parameters.add(tradingQuery.getCreateTimeStart());
				}
				if (tradingQuery.getCreateTimeEnd() != null) {
					s.append(" AND DATE(T6017.F09) <= ?");
					parameters.add(tradingQuery.getCreateTimeEnd());
				}
			}
		}
		EntrustType zt = tradingQuery.getWtzt();
		if (zt == null) {
			zt = EntrustType.JYZ;
		}
		if (tradingQuery.getJyztStatus() != null && tradingQuery.getJyztStatus() == JyztStatus.MR) {
			s.append(" AND T6017.F08=?   ORDER BY T6017.F04 DESC,T6017.F09 DESC  LIMIT 100");

		} else if (tradingQuery.getJyztStatus() != null && tradingQuery.getJyztStatus() == JyztStatus.MC) {
			s.append(" AND T6017.F08=? ORDER BY T6017.F04 ASC,T6017.F09 DESC  LIMIT 100");
		} else {
			s.append(" AND T6017.F08=? ORDER BY T6017.F09 DESC  LIMIT 100");
		}
		parameters.add(zt);
		return selectPaging(getConnection(P2PConst.DB_USER), new ArrayParser<JyglEntity>() {
			@Override
			public JyglEntity[] parse(ResultSet re) throws SQLException {
				ArrayList<JyglEntity> list = new ArrayList<JyglEntity>();
				while (re.next()) {
					JyglEntity jy = new JyglEntity();
					jy.id = re.getInt(1);
					jy.userName = re.getString(2);
					jy.dj = re.getBigDecimal(3);
					jy.sl = re.getBigDecimal(4);
					jy.ycj = re.getBigDecimal(5);
					jy.jyztStatus = EnumParser.parse(JyztStatus.class, re.getString(6));
					jy.entrustType = EnumParser.parse(EntrustType.class, re.getString(7));
					jy.sj = re.getTimestamp(8);
					jy.ywm_m = re.getString(9);
					jy.zwm_m = re.getString(10);
					jy.ywm_s = re.getString(11);
					jy.zwm_s = re.getString(12);
					jy.ze = jy.dj.multiply(jy.ycj).setScale(8, BigDecimal.ROUND_HALF_UP);
					if (list == null) {
						list = new ArrayList<>();
					}
					list.add(jy);
				}
				return list == null ? null : list.toArray(new JyglEntity[list.size()]);
			}
		}, paging, s.toString(), parameters);
	}

	@Override
	public JyfqEntity[] jyfqSearch() throws Throwable {
		return selectAll(getConnection(P2PConst.DB_USER), new ArrayParser<JyfqEntity>() {
			ArrayList<JyfqEntity> list = new ArrayList<JyfqEntity>();

			@Override
			public JyfqEntity[] parse(ResultSet re) throws SQLException {
				while (re.next()) {
					JyfqEntity j = new JyfqEntity();
					j.id = re.getInt(1);
					j.ywm_m = re.getString(2);
					j.zwm_m = re.getString(3);
					j.ywm_s = re.getString(4);
					j.zwm_s = re.getString(5);
					list.add(j);
				}
				return list == null ? null : list.toArray(new JyfqEntity[list.size()]);
			}
		}, "SELECT T6015.F01 id,mr.F03 ywm_c,mr.F04 zwm_c, mc.F03 ywm_r,mc.F04 zwm_r FROM T6015 LEFT JOIN T6013 mr ON T6015.F03=mr.F01 LEFT JOIN T6013 mc ON mc.F01=T6015.F04");
	}

	@Override
	public JyfqEntity[] jyfqOnlySearch() throws Throwable {
		return selectAll(getConnection(P2PConst.DB_USER), new ArrayParser<JyfqEntity>() {
			ArrayList<JyfqEntity> list = new ArrayList<JyfqEntity>();

			@Override
			public JyfqEntity[] parse(ResultSet re) throws SQLException {
				while (re.next()) {
					JyfqEntity j = new JyfqEntity();
					j.id = re.getInt(1);
					j.fqmc = re.getString(2);
					j.fqywm = re.getString(3);
					list.add(j);
				}
				return list == null ? null : list.toArray(new JyfqEntity[list.size()]);
			}
		}, "select F01,F03,F04 from T6014");
	}

	@Override
	public PagingResult<CjjlEntity> cjjlSearch(TradingQuery tradingQuery, Paging paging) throws Throwable {
		StringBuilder s = new StringBuilder(
				"SELECT T6018.F01 ID,T6018.F05 dj,T6018.F06 sl,T6018.F07 ze,T6018.F08 buyer_sxf,T6018.F09 seller_sxf, T6018.F10 lx,T6018.F11 sj, "
						+ "T6018.F03 buyer,T6018.F04 seller, "
						+ " mr.F03 ywm_c,mr.F04 zwm_c, mc.F03 ywm_r,mc.F04 zwm_r "
						+ "FROM T6018 LEFT JOIN T6015 ON T6018.F02=T6015.F01 "
						+ "LEFT JOIN T6010  ON T6018.F03=T6010.F01 " + " LEFT JOIN T6013 mr ON mr.F01=T6015.F03 "
						+ " LEFT JOIN T6013 mc ON mc.F01=T6015.F04 " + " LEFT JOIN T6010 mr_id ON mr_id.F01=T6018.F03 "
						+ " LEFT JOIN T6010 mc_id ON mc_id.F01=T6018.F04 " + " WHERE 1=1  AND T6018.F03<>T6018.F04 ");
		ArrayList<Object> parameters = new ArrayList<>();
		int userid = serviceResource.getSession().getAccountId();
		if (userid == 17) {
			s.append(" AND T6018.F02=6 ");
			s.append(" ORDER BY T6018.F11 DESC LIMIT 100");
		} else {
			if (tradingQuery != null) {
				SQLConnectionProvider sqlConnectionProvider = getSQLConnectionProvider();
				if (tradingQuery.getId() > 0) {
					s.append(" AND T6018.F02=?");
					parameters.add(tradingQuery.getId());
				}
				if (!StringHelper.isEmpty(tradingQuery.getBuyer())) {
					s.append(" AND mr_id.F02 LIKE ?");
					parameters.add(sqlConnectionProvider.allMatch(tradingQuery.getBuyer()));
				}
				if (!StringHelper.isEmpty(tradingQuery.getSeller())) {
					s.append(" AND mc_id.F02 LIKE ?");
					parameters.add(sqlConnectionProvider.allMatch(tradingQuery.getSeller()));
				}
				if (tradingQuery.getJyztStatus() != null) {
					s.append(" AND T6018.F10=?");
					parameters.add(tradingQuery.getJyztStatus());
				}
				if (tradingQuery.getCreateTimeStart() != null) {
					s.append(" AND TIMESTAMP(T6018.F11) >= ?");
					parameters.add(tradingQuery.getCreateTimeStart());
				}
				if (tradingQuery.getCreateTimeEnd() != null) {
					s.append(" AND TIMESTAMP(T6018.F11) <= ?");
					parameters.add(tradingQuery.getCreateTimeEnd());
				}
			}
			int i = 100;
			if (tradingQuery.getXsts() > 0) {
				i = tradingQuery.getXsts();
			}
			s.append(" ORDER BY T6018.F11 DESC LIMIT ?");
			parameters.add(i);
		}

		return selectPaging(getConnection(P2PConst.DB_USER), new ArrayParser<CjjlEntity>() {
			@Override
			public CjjlEntity[] parse(ResultSet re) throws SQLException {
				ArrayList<CjjlEntity> list = new ArrayList<CjjlEntity>();
				while (re.next()) {
					CjjlEntity jy = new CjjlEntity();
					jy.id = re.getInt(1);
					jy.dj = re.getBigDecimal(2);
					jy.sl = re.getBigDecimal(3);
					jy.ze = re.getBigDecimal(4);
					jy.buyer_sxf = re.getBigDecimal(5);
					jy.seller_sxf = re.getBigDecimal(6);
					jy.jyztStatus = EnumParser.parse(JyztStatus.class, re.getString(7));
					jy.sj = re.getTimestamp(8);
					jy.buyer = getUserName(re.getInt(9));
					jy.seller = getUserName(re.getInt(10));
					jy.ywm_m = re.getString(11);
					jy.zwm_m = re.getString(12);
					jy.ywm_s = re.getString(13);
					jy.zwm_s = re.getString(14);
					if (list == null) {
						list = new ArrayList<>();
					}
					list.add(jy);
				}
				return list == null ? null : list.toArray(new CjjlEntity[list.size()]);
			}
		}, paging, s.toString(), parameters);
	}

	private String getUserName(int userid) throws SQLException {
		String username = "";
		if (userid <= 0) {
			return username;
		}
		username = selectString(P2PConst.DB_USER, "SELECT F02 FROM T6010 WHERE F01=?", userid);
		return username;
	}

	@Override
	public PagingResult<XlbEntity> xlbSearch(XlbQuery query, Paging paging) throws Throwable {
		StringBuffer s = new StringBuffer(
				"SELECT F01,F02,F03,F04,F05,F06,F07,F08,F09,F10,F11,F12,F13,F14,F15,F16,F17,F18,F19,F20,F21,F23,F24,F25,F26,F27 FROM T6013 WHERE 1=1 ");
		ArrayList<Object> parameters = new ArrayList<Object>();
		if (query != null) {
			SQLConnectionProvider sqlConnectionProvider = getSQLConnectionProvider();

			if (!StringHelper.isEmpty(query.getZwm())) {
				s.append(" AND T6013.F04 LIKE ?");
				parameters.add(sqlConnectionProvider.allMatch(query.getZwm()));
			}
			if (query.getCreateTimeStart() != null) {
				s.append(" AND DATE(T6013.F17) >= ?");
				parameters.add(query.getCreateTimeStart());
			}
			if (query.getCreateTimeEnd() != null) {
				s.append(" AND DATE(T6013.F17) <= ?");
				parameters.add(query.getCreateTimeEnd());
			}
		}
		return selectPaging(getConnection(P2PConst.DB_USER), new ArrayParser<XlbEntity>() {
			@Override
			public XlbEntity[] parse(ResultSet re) throws SQLException {
				ArrayList<XlbEntity> list = new ArrayList<XlbEntity>();
				while (re.next()) {
					XlbEntity x = new XlbEntity();
					x.id = re.getInt(1);
					x.ywjc = re.getString(2);
					x.ywm = re.getString(3);
					x.zwm = re.getString(4);
					x.tblj = re.getString(5);
					x.zc_zr = EnumParser.parse(ZrState.class, re.getString(6));
					x.zc_zc = EnumParser.parse(IsPass.class, re.getString(7));
					x.zx_xz = re.getBigDecimal(8);
					x.zd_xz = re.getBigDecimal(9);
					x.zd_sxf = re.getBigDecimal(10);
					x.zg_sxf = re.getBigDecimal(11);
					x.r_qbdz = re.getString(12);
					x.r_qbsy = re.getString(13);
					x.l_qbdz = re.getString(14);
					x.sfzdzrlqb = EnumParser.parse(IsPass.class, re.getString(15));
					x.dy_zdzr = re.getString(16);
					x.fbsj = re.getTimestamp(17);
					x.wallet_ip = re.getString(18);
					x.wallet_port = re.getString(19);
					x.wallet_username = re.getString(20);
					x.wallet_password = re.getString(21);
					x.methodId = re.getString(22);
					x.contractAddress = re.getString(23);
					x.unit = re.getString(24);
					x.coinType = re.getString(25);
					x.tranHotWallet = re.getString(26);
					if (list == null) {
						list = new ArrayList<>();
					}
					list.add(x);
				}
				return list == null ? null : list.toArray(new XlbEntity[list.size()]);
			}
		}, paging, s.toString(), parameters);
	}

	@Override
	public XlbEntity getXlb(int id) throws Throwable {
		if (id <= 0) {
			throw new ParameterException("ID异常。");
		}
		StringBuffer s = new StringBuffer(
				"SELECT F01,F02,F03,F04,F05,F06,F07,F08,F09,F10,F11,F12,F13,F14,F15,F16,F17,F18,F19,F20,F21,F22,F23,F24,F25,F26,F27,F28,F29,F30,F31 FROM T6013 WHERE F01=? ");
		return select(getConnection(P2PConst.DB_USER), new ItemParser<XlbEntity>() {

			@Override
			public XlbEntity parse(ResultSet re) throws SQLException {
				XlbEntity x = new XlbEntity();
				while (re.next()) {
					x.id = re.getInt(1);
					x.ywjc = re.getString(2);
					x.ywm = re.getString(3);
					x.zwm = re.getString(4);
					x.tblj = re.getString(5);
					x.zc_zr = EnumParser.parse(ZrState.class, re.getString(6));
					x.zc_zc = EnumParser.parse(IsPass.class, re.getString(7));
					x.zx_xz = re.getBigDecimal(8);
					x.zd_xz = re.getBigDecimal(9);
					x.zd_sxf = re.getBigDecimal(10);
					x.zg_sxf = re.getBigDecimal(11);
					x.r_qbdz = re.getString(12);
					x.r_qbsy = re.getString(13);
					x.l_qbdz = re.getString(14);
					x.sfzdzrlqb = EnumParser.parse(IsPass.class, re.getString(15));
					x.dy_zdzr = re.getString(16);
					x.fbsj = re.getTimestamp(17);
					x.wallet_ip = re.getString(18);
					x.wallet_port = re.getString(19);
					x.wallet_username = re.getString(20);
					x.wallet_password = re.getString(21);
					x.tcrq = re.getTimestamp(22);
					x.methodId = re.getString(23);
					x.contractAddress = re.getString(24);
					x.unit = re.getString(25);
					x.coinType = re.getString(26);
					x.tranHotWallet = re.getString(27);
					x.coinNameTw = re.getString(28);
					x.coinDesc = re.getString(29);
					x.coinDescEn = re.getString(30);
					x.coinDescTw = re.getString(31);
				}
				return x;
			}
		}, s.toString(), id);
	}

	@Override
	public void addXlb(XlbQuery query) throws Throwable {
		String imageCode;
		if (query != null) {
			if (StringHelper.isEmpty(query.getYwjc())) {
				throw new ParameterException("英文简称不能为空。");
			}
			if (StringHelper.isEmpty(query.getYwm())) {
				throw new ParameterException("英文名不能为空。");
			}
			if (StringHelper.isEmpty(query.getZwm())) {
				throw new ParameterException("中文名不能为空。");
			}
			if (StringHelper.isEmpty(query.getR_qbdz())) {
				throw new ParameterException("热钱包地址不能为空。");
			}
			if (StringHelper.isEmpty(query.getR_qbsy())) {
				throw new ParameterException("热钱包私钥不能为空。");
			}
			if (StringHelper.isEmpty(query.getYwjc())) {
				throw new ParameterException("冷钱包地址不能为空。");
			}
			if (query.getTcrq() == null) {
				throw new ParameterException("推出日期不能为空。");
			}
		}
		UploadFile image = query.getTblj();
		if (image == null) {
			throw new ParameterException("图标不能为空");
		}
		String sy = MyCrypt.myEncode(query.getR_qbsy());
		FileStore fileStore = serviceResource.getResource(FileStore.class);
		imageCode = fileStore.upload(FileType.VIRTUAL_CURRENCY_IMAGE.ordinal(), image)[0];
		execute(getConnection(P2PConst.DB_USER),
				"INSERT INTO T6013 SET F02=?,F03=?,F04=?,F05=?,F06=?,F07=?,F08=?,F09=?,F10=?,F11=?,F12=?,F13=?,F14=?,F15=?,F16=?,F17=CURRENT_TIMESTAMP(),F18=?,F19=?,F20=?,F21=?,F22=?,F23=?,F24=?,F25=?,F26=?,F27=?,F28=?,F29=?,F30=?,F31=?",
				query.getYwjc(), query.getYwm(), query.getZwm(), imageCode, query.getZc_zr(), query.getZc_zc(),
				query.getZx_xz(), query.getZd_xz(), query.getZd_sxf(), query.getZg_sxf(), query.getR_qbdz(), sy,
				query.getL_qbdz(), query.getSfzdzrlqb(), query.getDy_zdzr(), query.getWalletIp(), query.getWalletPort(),
				query.getWalletUsername(), query.getWalletPassword(), query.getTcrq(), query.getMethodId(),
				query.getContractAddress(), query.getUnit(), query.getCoinType(), query.getTranHotWallet(),
				query.getCoinNameTw(), query.getCoinDesc(), query.getCoinDescEn(), query.getCoinDescTw());
	}

	@Override
	public void updateXlb(XlbQuery query, int id) throws Throwable {
		if (id <= 0) {
			throw new ParameterException("ID异常。");
		}
		String imageCode;
		if (query != null) {
			if (StringHelper.isEmpty(query.getYwjc())) {
				throw new ParameterException("英文简称不能为空。");
			}
			if (StringHelper.isEmpty(query.getYwm())) {
				throw new ParameterException("英文名不能为空。");
			}
			if (StringHelper.isEmpty(query.getZwm())) {
				throw new ParameterException("中文名不能为空。");
			}
			if (StringHelper.isEmpty(query.getR_qbdz())) {
				throw new ParameterException("热钱包地址不能为空。");
			}
			if (StringHelper.isEmpty(query.getYwjc())) {
				throw new ParameterException("冷钱包地址不能为空。");
			}
		}
		UploadFile image = query.getTblj();

		if (image != null) {
			FileStore fileStore = serviceResource.getResource(FileStore.class);
			imageCode = fileStore.upload(FileType.VIRTUAL_CURRENCY_IMAGE.ordinal(), image)[0];
			execute(getConnection(P2PConst.DB_USER),
					"UPDATE T6013 SET F02=?,F03=?,F04=?,F05=?,F06=?,F07=?,F08=?,F09=?,F10=?,F11=?,F12=?,F14=?,F15=?,F16=?,F17=CURRENT_TIMESTAMP(),F18=?,F19=?,F20=?,F21=?,F22=?,F23=?,F24=?,F25=?,F26=?,F27=?,F28=?,F29=?,F30=?,F31=? WHERE F01=? ",
					query.getYwjc(), query.getYwm(), query.getZwm(), imageCode, query.getZc_zr(), query.getZc_zc(),
					query.getZx_xz(), query.getZd_xz(), query.getZd_sxf(), query.getZg_sxf(), query.getR_qbdz(),
					query.getL_qbdz(), query.getSfzdzrlqb(), query.getDy_zdzr(), query.getWalletIp(),
					query.getWalletPort(), query.getWalletUsername(), query.getWalletPassword(), query.getTcrq(),
					query.getMethodId(), query.getContractAddress(), query.getUnit(), query.getCoinType(),
					query.getTranHotWallet(), query.getCoinNameTw(), query.getCoinDesc(), query.getCoinDescEn(),
					query.getCoinDescTw(), id);
		} else {
			System.out.println(query.getCoinType());
			execute(getConnection(P2PConst.DB_USER),
					"UPDATE T6013 SET F02=?,F03=?,F04=?,F06=?,F07=?,F08=?,F09=?,F10=?,F11=?,F12=?,F14=?,F15=?,F16=?,F17=CURRENT_TIMESTAMP(),F18=?,F19=?,F20=?,F21=?,F22=?,F23=?,F24=?,F25=?,F26=?,F27=?,F28=?,F29=?,F30=?,F31=? WHERE F01=? ",
					query.getYwjc(), query.getYwm(), query.getZwm(), query.getZc_zr(), query.getZc_zc(),
					query.getZx_xz(), query.getZd_xz(), query.getZd_sxf(), query.getZg_sxf(), query.getR_qbdz(),
					query.getL_qbdz(), query.getSfzdzrlqb(), query.getDy_zdzr(), query.getWalletIp(),
					query.getWalletPort(), query.getWalletUsername(), query.getWalletPassword(), query.getTcrq(),
					query.getMethodId(), query.getContractAddress(), query.getUnit(), query.getCoinType(),
					query.getTranHotWallet(), query.getCoinNameTw(), query.getCoinDesc(), query.getCoinDescEn(),
					query.getCoinDescTw(), id);
		}
	}

	@Override
	public void deleteXlb(int id) throws Throwable {
		if (id <= 0) {
			throw new ParameterException("ID异常。");
		}
		execute(getConnection(P2PConst.DB_USER), "DELETE FROM T6013 WHERE F01=?", id);
		log(serviceResource.getSession().getAccountId(), "删除虚拟币：" + id);
	}

	@Override
	public PagingResult<JyfqEntity> jyfqSearch(Paging paging) throws Throwable {
		return selectPaging(getConnection(P2PConst.DB_USER), new ArrayParser<JyfqEntity>() {
			@Override
			public JyfqEntity[] parse(ResultSet re) throws SQLException {
				ArrayList<JyfqEntity> list = new ArrayList<JyfqEntity>();
				while (re.next()) {
					JyfqEntity x = new JyfqEntity();
					x.id = re.getInt(1);
					x.bid = re.getInt(2);
					x.fqmc = re.getString(3);
					x.fqywm = re.getString(4);
					x.isPass = EnumParser.parse(IsPass.class, re.getString(5));
					x.px = re.getInt(6);
					x.sj = re.getTimestamp(7);
					x.ywjc = re.getString(8);
					x.bzzwm = re.getString(9);
					if (list == null) {
						list = new ArrayList<>();
					}
					list.add(x);
				}
				return list == null ? null : list.toArray(new JyfqEntity[list.size()]);
			}
		}, paging,
				"SELECT T6014.F01,T6014.F02 bid,T6014.F03,T6014.F04 fqywm,T6014.F05,T6014.F06,T6014.F07,T6013.F02 b_ywm,T6013.F04 b_zwm FROM T6014 LEFT JOIN T6013 ON T6014.F02=T6013.F01  ORDER BY T6013.F06 DESC,T6013.F07 DESC ");
	}

	@Override
	public void addJyfq(JyfqQuery query) throws Throwable {
		if (query != null) {
			if (query.getBid() <= 0) {
				throw new ParameterException("虚拟币ID异常。");
			}
			if (query.getPx() <= 0) {
				throw new ParameterException("排序值不能小于0。");
			}
			if (StringHelper.isEmpty(query.getFqmc())) {
				throw new ParameterException("分区名称不能为空。");
			}
			if (StringHelper.isEmpty(query.getFqywm())) {
				throw new ParameterException("分区英文名不能为空。");
			}
		}
		execute(getConnection(P2PConst.DB_USER),
				"INSERT INTO T6014 SET F02=?,F03=?,F04=?,F05=?,F06=?,F07=CURRENT_TIMESTAMP()", query.getBid(),
				query.getFqmc(), query.getFqywm(), query.sfky(), query.getPx());

	}

	@Override
	public JyfqEntity getJyfq(int fqid) throws Throwable {
		if (fqid <= 0) {
			throw new ParameterException("ID异常。");
		}
		return select(getConnection(P2PConst.DB_USER), new ItemParser<JyfqEntity>() {

			@Override
			public JyfqEntity parse(ResultSet re) throws SQLException {
				JyfqEntity x = new JyfqEntity();
				while (re.next()) {
					x.id = re.getInt(1);
					x.bid = re.getInt(2);
					x.fqmc = re.getString(3);
					x.fqywm = re.getString(4);
					x.isPass = EnumParser.parse(IsPass.class, re.getString(5));
					x.px = re.getInt(6);
					x.sj = re.getTimestamp(7);
				}

				return x;
			}
		}, "SELECT F01,F02,F03,F04,F05,F06,F07 FROM T6014 WHERE F01=?", fqid);
	}

	@Override
	public void updateJyfq(JyfqQuery query, int fqid) throws Throwable {
		if (fqid <= 0) {
			throw new ParameterException("ID异常。");
		}
		if (query != null) {
			if (query.getBid() <= 0) {
				throw new ParameterException("虚拟币ID异常。");
			}
			if (query.getPx() <= 0) {
				throw new ParameterException("排序值不能小于0。");
			}
			if (StringHelper.isEmpty(query.getFqmc())) {
				throw new ParameterException("分区名称不能为空。");
			}
			if (StringHelper.isEmpty(query.getFqywm())) {
				throw new ParameterException("分区英文名不能为空。");
			}
		}
		execute(getConnection(P2PConst.DB_USER),
				"UPDATE T6014 SET F02=?,F03=?,F04=?,F05=?,F06=?,F07=CURRENT_TIMESTAMP() WHERE F01=?", query.getBid(),
				query.getFqmc(), query.getFqywm(), query.sfky(), query.getPx(), fqid);

	}

	@Override
	public void deleteJyfq(int fqid) throws Throwable {
		if (fqid <= 0) {
			throw new ParameterException("ID异常。");
		}
		execute(getConnection(P2PConst.DB_USER), "DELETE FROM T6014 WHERE F01=?", fqid);
		log(serviceResource.getSession().getAccountId(), "删除交易分区：" + fqid);
	}

	@Override
	public XlbEntity[] getXlbxx() throws Throwable {
		return selectAll(getConnection(P2PConst.DB_USER), new ArrayParser<XlbEntity>() {
			ArrayList<XlbEntity> list = new ArrayList<XlbEntity>();

			@Override
			public XlbEntity[] parse(ResultSet re) throws SQLException {
				while (re.next()) {
					XlbEntity x = new XlbEntity();
					x.id = re.getInt(1);
					x.ywjc = re.getString(2);
					x.zwm = re.getString(3);
					if (list == null) {
						list = new ArrayList<>();
					}
					list.add(x);
				}
				return list == null ? null : list.toArray(new XlbEntity[list.size()]);
			}
		}, "SELECT F01,F02,F04 FROM T6013 ");
	}

	@Override
	public void sfky(int id, IsPass is) throws Throwable {
		execute(getConnection(P2PConst.DB_USER), "UPDATE T6014 SET F05=? WHERE F01=?", is, id);
		log(serviceResource.getSession().getAccountId(), "交易分区状态修改：" + id + ">" + is);

	}

	@Override
	public PagingResult<JyscEntity> jyscSearch(JyscQuery query, Paging paging) throws Throwable {
		StringBuilder s = new StringBuilder(
				"SELECT T6015.F01 id,T6015.F02 fqid,T6015.F03 mr_bz,T6015.F04 mc_bz,T6015.F05 xsws,T6015.F06 mr_sxf,");
		s.append(
				" T6015.F07 mc_scf,T6015.F08 mr_zxj,T6015.F09 mr_zdj,T6015.F10 mc_zxj,T6015.F11 mc_zdj,T6015.F12 sfkqjy,T6015.F13 sfxs,T6015.F14 sj,");
		s.append(" mr.F03 ywm_c,mr.F04 zwm_c, mc.F03 ywm_r,mc.F04 zwm_r , T6015.F15");
		s.append(" FROM T6015  ");
		s.append(" LEFT JOIN T6013 mr ON mr.F01=T6015.F03 ");
		s.append(" LEFT JOIN T6013 mc ON mc.F01=T6015.F04 ");
		s.append(" WHERE 1=1");
		ArrayList<Object> parameters = new ArrayList<Object>();
		if (query != null) {
			if (query.getCreateTimeStart() != null) {
				s.append(" AND DATE(T6015.F14) >= ?");
				parameters.add(query.getCreateTimeStart());
			}
			if (query.getCreateTimeEnd() != null) {
				s.append(" AND DATE(T6015.F14) <= ?");
				parameters.add(query.getCreateTimeEnd());
			}
		}
		s.append(" ORDER BY T6015.F14 DESC");
		return selectPaging(getConnection(P2PConst.DB_USER), new ArrayParser<JyscEntity>() {
			@Override
			public JyscEntity[] parse(ResultSet re) throws SQLException {
				ArrayList<JyscEntity> list = new ArrayList<JyscEntity>();
				while (re.next()) {
					JyscEntity x = new JyscEntity();
					x.id = re.getInt(1);
					x.fqid = re.getInt(2);
					x.mr_bz = re.getInt(3);
					x.mc_bz = re.getInt(4);
					x.xsws = re.getInt(5);
					x.mr_sxf = re.getBigDecimal(6);
					x.mc_sxf = re.getBigDecimal(7);
					x.mr_zxj = re.getBigDecimal(8);
					x.mr_zdj = re.getBigDecimal(9);
					x.mc_zxj = re.getBigDecimal(10);
					x.mc_zdj = re.getBigDecimal(11);
					x.sfkqjy = EnumParser.parse(IsPass.class, re.getString(12));
					x.sfxs = EnumParser.parse(IsPass.class, re.getString(13));
					x.sj = re.getTimestamp(14);
					x.ywm_m = re.getString(15);
					x.zwm_m = re.getString(16);
					x.ywm_s = re.getString(17);
					x.zwm_s = re.getString(18);
					x.slxsws = re.getInt(19);
					list.add(x);
				}
				return list == null ? null : list.toArray(new JyscEntity[list.size()]);
			}
		}, paging, s.toString(), parameters);
	}

	@Override
	public JyscEntity getJysc(int id) throws Throwable {
		if (id <= 0) {
			throw new ParameterException("ID异常。");
		}
		StringBuilder s = new StringBuilder(
				"SELECT T6015.F01 id,T6015.F02 fqid,T6015.F03 mr_bz,T6015.F04 mc_bz,T6015.F05 xsws,T6015.F06 mr_sxf,");
		s.append(
				" T6015.F07 mc_scf,T6015.F08 mr_zxj,T6015.F09 mr_zdj,T6015.F10 mc_zxj,T6015.F11 mc_zdj,T6015.F12 sfkqjy,T6015.F13 sfxs,T6015.F14 sj,");
		s.append(" T6014.F03 fqzwm,T6014.F04 fqywm,T6015.F15");
		s.append(" FROM T6015 LEFT JOIN T6014 ON T6015.F02=T6014.F01 WHERE T6015.F01=?");
		return select(getConnection(P2PConst.DB_USER), new ItemParser<JyscEntity>() {
			JyscEntity x = new JyscEntity();

			@Override
			public JyscEntity parse(ResultSet re) throws SQLException {
				while (re.next()) {
					x.id = re.getInt(1);
					x.fqid = re.getInt(2);
					x.mr_bz = re.getInt(3);
					x.mc_bz = re.getInt(4);
					x.xsws = re.getInt(5);
					x.mr_sxf = re.getBigDecimal(6);
					x.mc_sxf = re.getBigDecimal(7);
					x.mr_zxj = re.getBigDecimal(8);
					x.mr_zdj = re.getBigDecimal(9);
					x.mc_zxj = re.getBigDecimal(10);
					x.mc_zdj = re.getBigDecimal(11);
					x.sfkqjy = EnumParser.parse(IsPass.class, re.getString(12));
					x.sfxs = EnumParser.parse(IsPass.class, re.getString(13));
					x.sj = re.getTimestamp(14);
					x.scfq_zwm = re.getString(15);
					x.scfq_ywm = re.getString(16);
					x.slxsws = re.getInt(17);
				}
				return x;
			}
		}, s.toString(), id);
	}

	@Override
	public void addJysc(JyscQuery query) throws Throwable {
		if (query != null) {
			if (query.getFqid() <= 0) {
				throw new ParameterException("分区ID不能小于0。");
			}
			if (query.getMc_bz() <= 0) {
				throw new ParameterException("买方币种ID不能小于0。");
			}
			if (query.getMc_bz() <= 0) {
				throw new ParameterException("卖方币种ID不能小于0。");
			}
			if (query.getXsws() < 0) {
				throw new ParameterException("价格小数位数不能小于0。");
			}
			if (query.getSlxsws() < 0) {
				throw new ParameterException("数量小数位数不能小于0。");
			}
			if (query.getMr_sxf().compareTo(new BigDecimal(0)) < 0) {
				throw new ParameterException("买入手续费率不能小于0。");
			}
			if (query.getMc_sxf().compareTo(new BigDecimal(0)) < 0) {
				throw new ParameterException("卖出手续费率不能小于0。");
			}
			if (query.getMr_zxj().compareTo(new BigDecimal(0)) < 0) {
				throw new ParameterException("买入最小交易价不能小于0。");
			}
			if (query.getMr_zdj().compareTo(new BigDecimal(0)) < 0) {
				throw new ParameterException("买入最大交易价不能小于0。");
			}
			if (query.getMc_zxj().compareTo(new BigDecimal(0)) < 0) {
				throw new ParameterException("卖出最小交易价不能小于0。");
			}
			if (query.getMc_zdj().compareTo(new BigDecimal(0)) < 0) {
				throw new ParameterException("卖出最大交易价不能小于0。");
			}
			if (query.getSfkqjy() == null) {
				throw new ParameterException("是否开启交易不能为空。");
			}
			if (query.getSfxs() == null) {
				throw new ParameterException("是否显示不能为空。");
			}
		}
		execute(getConnection(P2PConst.DB_USER),
				"INSERT INTO T6015 SET F02=?,F03=?,F04=?,F05=?,F06=?,F07=?,F08=?,F09=?,F10=?,F11=?,F12=?,F13=?,F14=CURRENT_TIMESTAMP(),F15=?",
				query.getFqid(), query.getMr_bz(), query.getMc_bz(), query.getXsws(), query.getMr_sxf(),
				query.getMc_sxf(), query.getMr_zxj(), query.getMr_zdj(), query.getMc_zxj(), query.getMc_zdj(),
				query.getSfkqjy(), query.getSfkqjy(), query.getSlxsws());

	}

	@Override
	public void updateJysc(JyscQuery query, int id) throws Throwable {
		if (id <= 0) {
			throw new ParameterException("ID异常。");
		}
		if (query != null) {
			if (query.getFqid() <= 0) {
				throw new ParameterException("分区ID不能小于0。");
			}
			if (query.getMc_bz() <= 0) {
				throw new ParameterException("买方币种ID不能小于0。");
			}
			if (query.getMc_bz() <= 0) {
				throw new ParameterException("卖方币种ID不能小于0。");
			}
			if (query.getXsws() < 0) {
				throw new ParameterException("价格小数位数不能小于0。");
			}
			if (query.getSlxsws() < 0) {
				throw new ParameterException("数量小数位数不能小于0。");
			}
			if (query.getMr_sxf().compareTo(new BigDecimal(0)) < 0) {
				throw new ParameterException("买入手续费率不能小于0。");
			}
			if (query.getMc_sxf().compareTo(new BigDecimal(0)) < 0) {
				throw new ParameterException("卖出手续费率不能小于0。");
			}
			if (query.getMr_zxj().compareTo(new BigDecimal(0)) < 0) {
				throw new ParameterException("买入最小交易价不能小于0。");
			}
			if (query.getMr_zdj().compareTo(new BigDecimal(0)) < 0) {
				throw new ParameterException("买入最大交易价不能小于0。");
			}
			if (query.getMc_zxj().compareTo(new BigDecimal(0)) < 0) {
				throw new ParameterException("卖出最小交易价不能小于0。");
			}
			if (query.getMc_zdj().compareTo(new BigDecimal(0)) < 0) {
				throw new ParameterException("卖出最大交易价不能小于0。");
			}
			if (query.getSfkqjy() == null) {
				throw new ParameterException("是否开启交易不能为空。");
			}
			if (query.getSfxs() == null) {
				throw new ParameterException("是否显示不能为空。");
			}
		}
		execute(getConnection(P2PConst.DB_USER),
				"UPDATE T6015 SET F02=?,F03=?,F04=?,F05=?,F06=?,F07=?,F08=?,F09=?,F10=?,F11=?,F12=?,F13=?,F14=CURRENT_TIMESTAMP(),F15=? WHERE F01=?",
				query.getFqid(), query.getMr_bz(), query.getMc_bz(), query.getXsws(), query.getMr_sxf(),
				query.getMc_sxf(), query.getMr_zxj(), query.getMr_zdj(), query.getMc_zxj(), query.getMc_zdj(),
				query.getSfkqjy(), query.getSfkqjy(), query.getSlxsws(), id);
	}

	@Override
	public void deleteJysc(int id) throws Throwable {
		if (id <= 0) {
			throw new ParameterException("ID异常。");
		}
		execute(getConnection(P2PConst.DB_USER), "DELETE FROM T6015 WHERE F01=?", id);
		log(serviceResource.getSession().getAccountId(), "删除交易市场ID：" + id);
	}

	@Override
	public void kjjy(int id, IsPass is) throws Throwable {
		if (id <= 0) {
			throw new ParameterException("ID异常。");
		}
		if (is == null) {
			throw new ParameterException("请选择类型。");
		}
		execute(getConnection(P2PConst.DB_USER), "UPDATE T6015 SET F12=? WHERE F01=?", is, id);
		log(serviceResource.getSession().getAccountId(), "是否开启交易ID:" + id + ">" + is);
	}

	@Override
	public void sfxs(int id, IsPass is) throws Throwable {
		if (id <= 0) {
			throw new ParameterException("ID异常。");
		}
		if (is == null) {
			throw new ParameterException("请选择类型。");
		}
		execute(getConnection(P2PConst.DB_USER), "UPDATE T6015 SET F13=? WHERE F01=?", is, id);
		log(serviceResource.getSession().getAccountId(), "是否显示ID:" + id + ">" + is);
	}

	@Override
	public PagingResult<CyljEntity> cyljSearch(int bid, Paging paging) throws Throwable {
		if (bid <= 0) {
			throw new ParameterException("ID异常。");
		}
		return selectPaging(getConnection(P2PConst.DB_USER), new ArrayParser<CyljEntity>() {
			ArrayList<CyljEntity> list = new ArrayList<CyljEntity>();

			@Override
			public CyljEntity[] parse(ResultSet re) throws SQLException {
				while (re.next()) {
					CyljEntity x = new CyljEntity();
					x.id = re.getInt(1);
					x.bid = re.getInt(2);
					x.mc = re.getString(3);
					x.dz = re.getString(4);
					x.sfxs = EnumParser.parse(IsPass.class, re.getString(5));
					x.px = re.getInt(6);
					list.add(x);
				}
				return list == null ? null : list.toArray(new CyljEntity[list.size()]);
			}
		}, paging, "SELECT F01,F02,F03,F04,F05,F06 FROM  T6013_2 WHERE F02=? ORDER BY F06 DESC", bid);
	}

	@Override
	public CyljEntity getCylj(int id) throws Throwable {
		if (id <= 0) {
			throw new ParameterException("ID异常。");
		}
		return select(getConnection(P2PConst.DB_USER), new ItemParser<CyljEntity>() {

			@Override
			public CyljEntity parse(ResultSet re) throws SQLException {
				CyljEntity x = new CyljEntity();
				while (re.next()) {
					x.id = re.getInt(1);
					x.bid = re.getInt(2);
					x.mc = re.getString(3);
					x.dz = re.getString(4);
					x.sfxs = EnumParser.parse(IsPass.class, re.getString(5));
					x.px = re.getInt(6);
					x.linkNameOrParamEn = re.getString(7);
					x.linkNameOrParamTw = re.getString(8);
				}
				return x;
			}
		}, "SELECT F01,F02,F03,F04,F05,F06,F07,F08 FROM  T6013_2 WHERE F01=?", id);
	}

	@Override
	public void addCylj(int bid, CyljQuery query) throws Throwable {
		if (bid <= 0) {
			throw new ParameterException("ID异常。");
		}
		if (query != null) {
			if (StringHelper.isEmpty(query.getMc())) {
				throw new ParameterException("链接名称不能为空。");
			}
			if (StringHelper.isEmpty(query.getDz())) {
				throw new ParameterException("链接地址不能为空。");
			}
			if (query.getXs() == null) {
				throw new ParameterException("是否显示不能为空。");
			}
			if (query.getPx() < 0) {
				throw new ParameterException("排序值不能小于0。");
			}
		}
		execute(getConnection(P2PConst.DB_USER), "INSERT INTO T6013_2 SET F02=?,F03=?,F04=?,F05=?,F06=?,F07=?,F08=?",
				bid, query.getMc(), query.getDz(), query.getXs(), query.getPx(), query.getLinkNameOrParamEn(),
				query.getLinkNameOrParamTw());
	}

	@Override
	public void updateCylj(int id, int bid, CyljQuery query) throws Throwable {
		if (id <= 0) {
			throw new ParameterException("ID异常。");
		}
		if (bid <= 0) {
			throw new ParameterException("币不存在。");
		}
		if (query != null) {
			if (StringHelper.isEmpty(query.getMc())) {
				throw new ParameterException("链接名称不能为空。");
			}
			if (StringHelper.isEmpty(query.getDz())) {
				throw new ParameterException("链接地址不能为空。");
			}
			if (query.getXs() == null) {
				throw new ParameterException("是否显示不能为空。");
			}
			if (query.getPx() < 0) {
				throw new ParameterException("排序值不能小于0。");
			}
		}
		execute(getConnection(P2PConst.DB_USER),
				"UPDATE T6013_2 SET F02=?,F03=?,F04=?,F05=?,F06=?,F07=?,F08=? WHERE F01=?", bid, query.getMc(),
				query.getDz(), query.getXs(), query.getPx(), query.getLinkNameOrParamEn(), query.getLinkNameOrParamTw(),
				id);
	}

	@Override
	public void deletecylj(int id) throws Throwable {
		if (id <= 0) {
			throw new ParameterException("ID异常。");
		}

		execute(getConnection(P2PConst.DB_USER), "DELETE FROM T6013_2 WHERE F01=?", id);
	}

	@Override
	public PagingResult<CyljEntity> xxcsSearch(int bid, Paging paging) throws Throwable {
		if (bid <= 0) {
			throw new ParameterException("ID异常。");
		}
		return selectPaging(getConnection(P2PConst.DB_USER), new ArrayParser<CyljEntity>() {
			ArrayList<CyljEntity> list = new ArrayList<CyljEntity>();

			@Override
			public CyljEntity[] parse(ResultSet re) throws SQLException {
				while (re.next()) {
					CyljEntity x = new CyljEntity();
					x.id = re.getInt(1);
					x.bid = re.getInt(2);
					x.sfdx = EnumParser.parse(IsPass.class, re.getString(3));
					x.nr = re.getString(4);
					x.sfxs = EnumParser.parse(IsPass.class, re.getString(5));
					x.px = re.getInt(6);
					list.add(x);
				}
				return list == null ? null : list.toArray(new CyljEntity[list.size()]);
			}
		}, paging, "SELECT F01,F02,F03,F04,F05,F06 FROM  T6013_1 WHERE F02=? ORDER BY F06 DESC", bid);
	}

	@Override
	public CyljEntity getXxcs(int id) throws Throwable {
		if (id <= 0) {
			throw new ParameterException("ID异常。");
		}
		return select(getConnection(P2PConst.DB_USER), new ItemParser<CyljEntity>() {

			@Override
			public CyljEntity parse(ResultSet re) throws SQLException {
				CyljEntity x = new CyljEntity();
				while (re.next()) {
					x.id = re.getInt(1);
					x.bid = re.getInt(2);
					x.sfdx = EnumParser.parse(IsPass.class, re.getString(3));
					x.nr = re.getString(4);
					x.sfxs = EnumParser.parse(IsPass.class, re.getString(5));
					x.px = re.getInt(6);
					x.linkNameOrParamEn = re.getString(7);
					x.linkNameOrParamTw = re.getString(8);
				}
				return x;
			}
		}, "SELECT F01,F02,F03,F04,F05,F06,F07,F08 FROM  T6013_1 WHERE F01=?", id);
	}

	@Override
	public void addXxcs(int bid, CyljQuery query) throws Throwable {
		if (bid <= 0) {
			throw new ParameterException("ID异常。");
		}
		if (query != null) {
			if (StringHelper.isEmpty(query.getNr())) {
				throw new ParameterException("内容不能为空。");
			}
			if (query.getXs() == null) {
				throw new ParameterException("是否显示不能为空。");
			}
			if (query.getSfdxxs() == null) {
				throw new ParameterException("是否单行显示不能为空。");
			}
			if (query.getPx() < 0) {
				throw new ParameterException("排序值不能小于0。");
			}
		}
		execute(getConnection(P2PConst.DB_USER), "INSERT INTO T6013_1 SET F02=?,F03=?,F04=?,F05=?,F06=?,F07=?,F08=?",
				bid, query.getSfdxxs(), query.getNr(), query.getXs(), query.getPx(), query.getLinkNameOrParamEn(),
				query.getLinkNameOrParamTw());
	}

	@Override
	public void updateXxcs(int id, int bid, CyljQuery query) throws Throwable {
		if (id <= 0) {
			throw new ParameterException("ID异常。");
		}
		if (bid <= 0) {
			throw new ParameterException("币不存在。");
		}
		if (query != null) {
			if (StringHelper.isEmpty(query.getNr())) {
				throw new ParameterException("内容不能为空。");
			}
			if (query.getXs() == null) {
				throw new ParameterException("是否显示不能为空。");
			}
			if (query.getSfdxxs() == null) {
				throw new ParameterException("是否单行显示不能为空。");
			}
			if (query.getPx() < 0) {
				throw new ParameterException("排序值不能小于0。");
			}
		}
		execute(getConnection(P2PConst.DB_USER),
				"UPDATE T6013_1 SET F02=?,F03=?,F04=?,F05=?,F06=?,F07=?,F08=? WHERE F01=?", bid, query.getSfdxxs(),
				query.getNr(), query.getXs(), query.getPx(), query.getLinkNameOrParamEn(), query.getLinkNameOrParamTw(),
				id);
	}

	@Override
	public void deleteXxcs(int id) throws Throwable {
		if (id <= 0) {
			throw new ParameterException("ID异常。");
		}

		execute(getConnection(P2PConst.DB_USER), "DELETE FROM T6013_1 WHERE F01=?", id);

	}

	@Override
	public void export(JyglEntity[] bzc, OutputStream outputStream, String charset) throws Throwable {
		if (outputStream == null) {
			return;
		}
		if (StringHelper.isEmpty(charset)) {
			charset = "GBK";
		}
		if (bzc == null) {
			return;
		}
		try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, charset))) {
			writer.write("用户ID");
			writer.write(",");
			writer.write("用户名");
			writer.write(",");
			writer.write("市场");
			writer.write(",");
			writer.write("单价");
			writer.write(",");
			writer.write("数量");
			writer.write(",");
			writer.write("已成交");
			writer.write(",");
			writer.write("总额");
			writer.write(",");
			writer.write("类型");
			writer.write(",");
			writer.write("状态");
			writer.write(",");
			writer.write("时间");
			writer.write(",");
			writer.newLine();
			for (JyglEntity w : bzc) {
				if (w == null) {
					continue;
				}
				writer.write(Integer.toString(w.id));
				writer.write(",");
				writer.write(w.userName == null ? "" : w.userName);
				writer.write(",");
				writer.write(w.zwm_s + "" + "(" + w.ywm_s + "" + ")/" + w.zwm_m + "" + "(" + w.ywm_m + "" + ")");
				writer.write(",");
				writer.write(format(w.dj).toString() + ";");
				writer.write(",");
				writer.write(format(w.sl).toString() + ";");
				writer.write(",");
				writer.write(format(w.ycj).toString() + ";");
				writer.write(",");
				writer.write(format(w.ze).toString() + ";");
				writer.write(",");
				if (w.jyztStatus != null) {
					writer.write(w.jyztStatus.getName());
				} else {
					writer.write("");
				}
				writer.write(",");
				if (w.entrustType != null) {
					writer.write(w.entrustType.getName());
				} else {
					writer.write("");
				}
				writer.write(",");
				writer.write(DateParser.format(w.sj, "yyyy-MM-dd HH:mm"));
				writer.newLine();
			}
			writer.flush();
		}
	}

	@Override
	public PagingResult<XlbEntity> xlbSearch() throws Throwable {
		StringBuffer s = new StringBuffer(
				"SELECT F01,F02,F03,F04,F05,F06,F07,F08,F09,F10,F11,F12,F13,F14,F15,F16,F17,F18,F19,F20,F21,F23,F24,F25,F26,F27 FROM T6013 WHERE F01>0 ");
		ArrayList<Object> parameters = new ArrayList<Object>();
		return selectPaging(getConnection(P2PConst.DB_USER), new ArrayParser<XlbEntity>() {
			@Override
			public XlbEntity[] parse(ResultSet re) throws SQLException {
				ArrayList<XlbEntity> list = new ArrayList<XlbEntity>();
				while (re.next()) {
					XlbEntity x = new XlbEntity();
					x.id = re.getInt(1);
					x.ywjc = re.getString(2);
					x.ywm = re.getString(3);
					x.zwm = re.getString(4);
					x.tblj = re.getString(5);
					x.zc_zr = EnumParser.parse(ZrState.class, re.getString(6));
					x.zc_zc = EnumParser.parse(IsPass.class, re.getString(7));
					x.zx_xz = re.getBigDecimal(8);
					x.zd_xz = re.getBigDecimal(9);
					x.zd_sxf = re.getBigDecimal(10);
					x.zg_sxf = re.getBigDecimal(11);
					x.r_qbdz = re.getString(12);
					x.r_qbsy = re.getString(13);
					x.l_qbdz = re.getString(14);
					x.sfzdzrlqb = EnumParser.parse(IsPass.class, re.getString(15));
					x.dy_zdzr = re.getString(16);
					x.fbsj = re.getTimestamp(17);
					x.wallet_ip = re.getString(18);
					x.wallet_port = re.getString(19);
					x.wallet_username = re.getString(20);
					x.wallet_password = re.getString(21);
					x.methodId = re.getString(22);
					x.contractAddress = re.getString(23);
					x.unit = re.getString(24);
					x.coinType = re.getString(25);
					x.tranHotWallet = re.getString(26);
					if (list == null) {
						list = new ArrayList<>();
					}
					list.add(x);
				}
				return list == null ? null : list.toArray(new XlbEntity[list.size()]);
			}
		}, new Paging() {

			@Override
			public int getSize() {
				return Integer.MAX_VALUE;
			}

			@Override
			public int getCurrentPage() {
				return 1;
			}
		}, s.toString(), parameters);
	}

	@Override
	public void updateXlbInfo(XlbQuery query, int id) throws Throwable {
		if (id <= 0) {
			throw new ParameterException("ID异常。");
		}
		String imageCode;
		UploadFile image = query.getTblj();

		if (image != null) {
			FileStore fileStore = serviceResource.getResource(FileStore.class);
			imageCode = fileStore.upload(FileType.VIRTUAL_CURRENCY_IMAGE.ordinal(), image)[0];
			execute(getConnection(P2PConst.DB_USER),
					"UPDATE T6013 SET F05=?,F17=CURRENT_TIMESTAMP(),F29=?,F30=?,F31=? WHERE F01=? ",
					imageCode, 
					query.getCoinDesc(), query.getCoinDescEn(),
					query.getCoinDescTw(), id);
		} else {
			System.out.println(query.getCoinType());
			execute(getConnection(P2PConst.DB_USER),
					"UPDATE T6013 SET F17=CURRENT_TIMESTAMP(),F29=?,F30=?,F31=? WHERE F01=? ",
					query.getCoinDesc(), query.getCoinDescEn(),
					query.getCoinDescTw(), id);
		}

	}
}
